unit buscarProveedor;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Provider, SqlExpr, DB, DBClient, DBLocal, DBLocalS, StdCtrls,
  Grids, DBGrids, RXDBCtrl, Buttons, DBCtrls, ExtCtrls;

type
  TfrmBuscarProveedor = class(TForm)
    grd: TRxDBGrid;
    Label4: TLabel;
    filtro: TEdit;
    cds: TSQLClientDataSet;
    s: TDataSource;
    cdsID_PROVEEDOR: TSmallintField;
    cdsRAZON: TStringField;
    cdsPROVINCIA: TStringField;
    cdsLOCALIDAD: TStringField;
    cdsCODIGO_POSTAL: TStringField;
    cdsCARACTERISTICA: TStringField;
    DBText1: TDBText;
    Label1: TLabel;
    Label2: TLabel;
    te: TLabel;
    Label3: TLabel;
    direccion: TLabel;
    cdsDOMICILIO: TStringField;
    Label5: TLabel;
    lblTotalRegistros: TLabel;
    Shape1: TShape;
    btnAbrir: TSpeedButton;
    Label6: TLabel;
    DBText2: TDBText;
    cdsCONTACTO: TStringField;
    Label7: TLabel;
    DBText3: TDBText;
    cdsEMAIL: TStringField;
    procedure sDataChange(Sender: TObject; Field: TField);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure filtroChange(Sender: TObject);
    procedure btnAbrirClick(Sender: TObject);
  private
    { Private declarations }
  public
    constructor CreateNuevo(AOwner: TComponent);
  end;

var
  frmBuscarProveedor: TfrmBuscarProveedor;

implementation

uses MDatos, abm, main;

{$R *.dfm}

{ TfrmBuscarProveedor }

constructor TfrmBuscarProveedor.CreateNuevo(AOwner: TComponent);
begin
  inherited Create(Aowner);
  self.ShowModal;
end;

procedure TfrmBuscarProveedor.sDataChange(Sender: TObject; Field: TField);
var
  ds: TSQLClientDataSet;
begin
  te.Caption := '';
  self.direccion.Caption := '';
  if (self.cdsID_PROVEEDOR.AsInteger <> 0) and not (self.cdsID_PROVEEDOR.IsNull) then
  begin
    self.direccion.Caption := self.cdsDOMICILIO.AsString+ ' - ('+self.cdsCODIGO_POSTAL.AsString+') '
    +self.cdsLOCALIDAD.AsString+' - '+self.cdsPROVINCIA.AsString;
    ds := TSQLClientDataSet.Create(self);
    try
      ds.DBConnection := Datos.Con;
      ds.CommandText := 'SELECT numero FROM te_proveedor WHERE id_proveedor = '
      +self.cdsID_PROVEEDOR.AsString;
      ds.Open;       
      if not ds.IsEmpty then
      begin
        while not ds.Eof do
        begin
          te.Caption := te.Caption+'('+self.cdsCARACTERISTICA.AsString+') '+ds.Fields[0].AsString+' - ';
          ds.Next;
        end;
        te.Caption := copy(te.Caption, 1, length(te.Caption)-3);
      end;
    finally
      ds.Free;
    end;
  end;
end;

procedure TfrmBuscarProveedor.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  Action := caFree;
end;

procedure TfrmBuscarProveedor.filtroChange(Sender: TObject);
begin
  if (self.cds.State in [dsInactive]) then
    self.cds.Open;
  self.cds.Filtered := False;
  if (self.filtro.Text <> '') then
  begin
    self.cds.Filter := 'RAZON LIKE ''%'+self.filtro.Text+'%''';
    self.cds.Filtered := True;
  end;
  self.lblTotalRegistros.Caption := IntToStr(self.cds.RecordCount);
end;

procedure TfrmBuscarProveedor.btnAbrirClick(Sender: TObject);
begin
  Datos.cProv.Open;
  Datos.cProv.Locate('ID_PROVEEDOR', self.cdsID_PROVEEDOR.AsInteger, []);
  TfrmABM.Entrar(self, frmMain.usuario, 3);
  self.cds.Refresh;
end;

end.
